<%@page import="org.springframework.security.core.context.SecurityContextHolder"%>
<%@page import="org.apache.commons.lang.StringUtils"%>
<%@page import="org.apache.commons.lang.time.DateFormatUtils"%>
<%@page import="org.springframework.util.CollectionUtils"%>
<%@page import="java.util.Collection"%>
<%@ include file="../common/common_include.jsp"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	UserInfo user = (UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
	String username = user.getName();
	String siteid = user.getSiteId();
	String defaultSiteId = request.getAttribute("defaultSiteid")+"";
%>

<div>
	<form  target="_coreIframe">
		<div class="breadcrumb">
			<div class="bcIcon"></div>
			<ul class="bcNav">
				<li>请假登记</li>
			</ul>
		</div>
		<table class="formTable">
			<tr>
				<td class="tdTitle">提示：</td>
				<td>
					<span>如果此页下面表单不显示请把IE浏览升级到IE8以上，或用火狐与谷歌浏览器</span>
				</td>
			</tr>
		</table>
		<table id="dg" class="easyui-datagrid" title="批量请假登记" style="width:1000px;height:600px" >			
		</table>
	</form>
	<iframe id="_coreIframe" name="_coreIframe" style="display: none;"></iframe>
</div>
<script>
	var date = new Date();
	var month = (date.getMonth()+1)+"";
	if(month.length == 1){
		month = "0"+month;
	}
	var year_month = date.getFullYear()+"-"+month;
	var siteid;
	var deptid;
	$('#dg').datagrid({
		rownumbers:true,
		iconCls: 'icon-edit',
		singleSelect: true,
		collapsible:true,
		columns: [[  
		             { title: '公司', field: 'siteid', width: 105,align:'center',
		            	 formatter:function(value,row){
								return row.sitename;
							},
		            	 editor:{
		            	 type:'combobox',
		            	 options:{
								valueField:'siteid',
								textField:'sitename',								
								method:'get',
								url:global.contextPath + '/company/siteList',
			                    onSelect:function(data){
			                    	var row = $('#dg').datagrid('getSelected');  
                                    var rowIndex = $('#dg').datagrid('getRowIndex',row);//获取行号  
                                    var thisTarget = $('#dg').datagrid('getEditor', {'index':rowIndex,'field':'siteid'}).target;  
                                    siteid = data.siteid;                                    
                                    var target0 = $('#dg').datagrid('getEditor', {'index':rowIndex,'field':'deptid'}).target;  
                                    //target0.combobox('clear'); //清除原来的数据  
                                    var target1 = $('#dg').datagrid('getEditor', {'index':rowIndex,'field':'userid'}).target;  
                                    //target1.combobox('clear'); //清除原来的数据  
                                    var url = global.contextPath + '/company/datagridDepts?siteid=' + siteid;  
                                    target0.combobox('reload', url);//联动下拉列表重载 
			                    },
			                    onChange:function(){
			                    	var row = $('#dg').datagrid('getSelected');  
                                    var rowIndex = $('#dg').datagrid('getRowIndex',row);//获取行号  
			                    	var target0 = $('#dg').datagrid('getEditor', {'index':rowIndex,'field':'deptid'}).target;  
                                    target0.combobox('clear'); //清除原来的数据  
                                    var target1 = $('#dg').datagrid('getEditor', {'index':rowIndex,'field':'userid'}).target;  
                                    target1.combobox('clear');
			                    },
			                    onHidePanel:function(){
									var _options = $(this).combobox('options');  
								    var _data = $(this).combobox('getData');/* 下拉框所有选项 */  
								    var _value = $(this).combobox('getValue');/* 用户输入的值 */  
								    var _b = false;/* 标识是否在下拉列表中找到了用户输入的字符 */  
								    for (var i = 0; i < _data.length; i++) {  
								        if (_data[i][_options.valueField] == _value) {  
								            _b=true;  
								            break;  
								        }  
								    }  
								    if(!_b){  
								        $(this).combobox('setValue', '');  
								    } 
			                    },
			                    panelHeight: '300',
								required: true
							}							
						}		             
		             },  
		             { title: '部门', field: 'deptid', width: 105,align:'center',
		            	 formatter:function(value,row){
								return row.deptname;
							},
		            	 editor:{
		            	 type:'combobox',
		            	 options:{
								valueField:'deptid',
								textField:'deptname',								
								method:'get',
								url:global.contextPath + '/company/datagridDepts?siteid=' + siteid,
								onSelect:function(data){
			                    	var row = $('#dg').datagrid('getSelected');  
                                    var rowIndex = $('#dg').datagrid('getRowIndex',row);//获取行号  
                                    var thisTarget = $('#dg').datagrid('getEditor', {'index':rowIndex,'field':'deptid'}).target;  
                                    deptid = data.deptid;
                                    var target = $('#dg').datagrid('getEditor', {'index':rowIndex,'field':'userid'}).target;  
                                    //target.combobox('clear'); //清除原来的数据 
                                    var url = global.contextPath + '/company/datagridUsers?siteid=' + siteid+'&deptid='+deptid;  
                                    target.combobox('reload', url);//联动下拉列表重载 
			                    },
			                    onChange:function(){
			                    	var row = $('#dg').datagrid('getSelected');  
                                    var rowIndex = $('#dg').datagrid('getRowIndex',row);//获取行号  
                                    var target1 = $('#dg').datagrid('getEditor', {'index':rowIndex,'field':'userid'}).target;  
                                    target1.combobox('clear');
			                    },
			                    onHidePanel:function(){
									var _options = $(this).combobox('options');  
								    var _data = $(this).combobox('getData');/* 下拉框所有选项 */  
								    var _value = $(this).combobox('getValue');/* 用户输入的值 */  
								    var _b = false;/* 标识是否在下拉列表中找到了用户输入的字符 */  
								    for (var i = 0; i < _data.length; i++) {  
								        if (_data[i][_options.valueField] == _value) {  
								            _b=true;  
								            break;  
								        }  
								    }  
								    if(!_b){  
								        $(this).combobox('setValue', '');  
								    } 
			                    },
			                    panelHeight: '300',
								required: true
							}
						} 
		             },  
		             { title: '员工', field: 'userid', width: 160,align:'center',
		            	 formatter:function(value,row){
								return row.usersname;
							},
		            	 editor:{
		            	 type:'combobox',
		            	 options:{
		            		 method:'get',
							 url:global.contextPath + '/company/datagridUsers?siteid=' + siteid+'&deptid='+deptid,
							 valueField:'userid',
							 textField:'username',
							 onHidePanel:function(){
									var _options = $(this).combobox('options');  
								    var _data = $(this).combobox('getData');/* 下拉框所有选项 */  
								    var _value = $(this).combobox('getValue');/* 用户输入的值 */  
								    var _b = false;/* 标识是否在下拉列表中找到了用户输入的字符 */  
								    for (var i = 0; i < _data.length; i++) {  
								        if (_data[i][_options.valueField] == _value) {  
								            _b=true;  
								            break;  
								        }  
								    }  
								    if(!_b){  
								        $(this).combobox('setValue', '');  
								    } 
			                    },
		                     panelHeight: '300',
							 required: true
						 }
		            	 
						} 
		             },  
					 { title: '开始日期', field: 'startDate', width: 150,align:'center' ,
						 editor:{
							type: 'datetimebox',
							options:{	
								showSeconds:false,	
								required: true
			            	 }
						 }
					 },
					 { title: '结束日期', field: 'endDate', width: 150,align:'center' ,
						 editor:{
							type: 'datetimebox',
							options:{
								showSeconds:false,
								required: true
			            	 }
						 }
					 },
					 { title: '请假类型', field: 'type', width: 80,align:'center',
		            	 formatter:function(value,row){
								return row.type;
							},
		            	 editor:{
		            		 type:'combobox',
			            	 options:{
			            		 valueField:'id',
								 textField:'id',
								 data:[{"id":"事假"},{"id":"病假"},{"id":"调休"},{"id":"出差"},{"id":"年假"},{"id":"工伤假"},{"id":"婚假"},{"id":"产假"},{"id":"陪产假"},{"id":"丧假"}],
								 panelHeight: 'auto',
								 required: true
			            	 } 
		            	 }
							
					 } ,
					 { title: '请假原因', field: 'reason', width: 200,align:'center',
		            	 editor:{
		            		 type:'textbox',
		            		 options:{
		            			 required: true
		            		 }
		            	 }
		             },
		        ]],
		toolbar:[{  
            id: 'btnAdd',  
            text: '<font size="3px">添加</font>',  
            iconCls: 'icon-add',
            handler: function () {  
            	append();//实现添加记录的页面  
            }
        }, '-', {  
            id: 'btnDelete',  
            text: '<font size="3px">删除</font>',  
            iconCls: 'icon-remove',  
            handler: function () {  
            	removeit();//实现直接删除数据的方法  
            }  
        }, '-', {  
            id: 'btnAccept',  
            text: '<font size="3px">提交</font>',  
            iconCls: 'icon-save',  
            handler: function () {  
            	accept();//实现直接提交数据的方法  
            }  
        }, '-', {  
            id: 'btnReject',  
            text: '<font size="3px">取消</font>',  
            iconCls: 'icon-undo',  
            handler: function () {  
            	reject();//取消  
            }  
        }],
        onClickCell: onClickCell,
		onEndEdit: onEndEdit
	});
	var editIndex = undefined;
	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#dg').datagrid('validateRow', editIndex)){
			$('#dg').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	}
	function onClickCell(index, field){
		if (editIndex != index){			
			if (endEditing()){
				$('#dg').datagrid('selectRow', index)
						.datagrid('beginEdit', index);
				var ed = $('#dg').datagrid('getEditor', {index:index,field:field});
				if (ed){
					($(ed.target).data('textbox') ? $(ed.target).textbox('textbox') : $(ed.target)).focus();
				}
				editIndex = index;
			} else {
				setTimeout(function(){
					$('#dg').datagrid('selectRow', editIndex);
				},0);
			}
		}
	}
	function onEndEdit(index, row){
		var siteed = $(this).datagrid('getEditor', {
			index: index,
			field: 'siteid'
		});
		var depted = $(this).datagrid('getEditor', {
			index: index,
			field: 'deptid'
		});
		var usersed = $(this).datagrid('getEditor', {
			index: index,
			field: 'userid'
		});
		row.sitename = $(siteed.target).combobox('getText');
		row.deptname = $(depted.target).combobox('getText');
		row.usersname = $(usersed.target).combobox('getText');
	}
	function append(){
		if (endEditing()){
			$('#dg').datagrid('appendRow',{reason:''});
			editIndex = $('#dg').datagrid('getRows').length-1;
			$('#dg').datagrid('selectRow', editIndex)
					.datagrid('beginEdit', editIndex);
		}
	}
	function removeit(){
		if (editIndex == undefined){return}
		$('#dg').datagrid('cancelEdit', editIndex)
				.datagrid('deleteRow', editIndex);
		editIndex = undefined;
	}
	
	function accept(){
		if (endEditing()){
			if($('#dg').datagrid("getRows").length >0){
				var leaveJson = JSON.stringify($('#dg').datagrid("getRows"));
				XF.Check.doAddPersonLeave(leaveJson,year_month,<%=defaultSiteId%>);
			}else{
				alert("表格无添加记录")
			}			
		}else{
			alert("红色必填项，时段必填其一。")
		}
		
	}
	function reject(){
		XF.Check.leaveList()
	}
	function getChanges(){
		var rows = $('#dg').datagrid('getChanges');
		alert(rows.length+' rows are changed!');
	}
</script>
<style type="text/css">
		.datagrid {margin:20px}
	</style>


